package com.turing.data.scripts.contract.transfer

import com.turing.common.util.Log
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO

import java.util.stream.Collectors

/*
* 标准化
*/

class Standardizing_30191_10_transfer {

    static void main(String[] args) {
    }

    static List<TaskElementVO.DrawResultVO> doConvert(List<TaskElementVO.DrawResultVO> resultVOs, DependParamsVO dependParams) {
        Log.info("Standardizing_9771_11_transfer_resultVOs={}",resultVOs)

        def REGEX_MAP = [
                "银行存款"           : /.*银行存款.*|.*存款.*/,
                "结构性存款"          : /.*结构性存款.*/,
                "同业存单"           : /.*同业存单.*/,
                "证券交易所债券、银行间市场债券": /.*国债/,
                "可交换债"           : /.*可交换债.*|.*可交债.*|.*可交换公司债.*/,
                "可转债"            : /.*可转债.*|.*可转换债券.*|.*可转换债.*|.*可转换公司债.*/,
                "央行票据"           : /.*央行票据.*|.*中央银行票据.*/,
                "短期融资券"          : /.*短期融资券.*/,
                "中期票据"           : /.*中期票据.*/,
                "资产支持证券"         : /.*资产支持证券.*|.*ABS.*/,
                "非金融企业债务融资工具"    : /.*非金融企业债务融资工具.*/,
                "股票一级"           : /.*股票一级.*|.*一级.*/,
                "股票定增"           : /.*股票定增.*|.*定增.*|.*定向增发.*/,
                "股票二级"           : /.*股票二级.*|.*二级.*/,
                "优先股"            : /.*优先股.*/,
                "港股通"            : /.*沪港通.*|.*深港通.*/,
                "新三板股票"          : /.*新三板股票.*|.*新三板.*/,
                "股指期货"           : /.*股指期货.*/,
                "国债期货"           : /.*国债期货.*/,
                "商品期货"           : /.*商品期货.*/,
                "场内期权"           : /.*场内期权.*/,
                "公募基金份额（不含货币基金）" : /.*股票型基金.*|.*债券型基金.*|.*混合型基金.*/,
                "货币基金"           : /.*货币基金.*|.*货币型基金.*|.*所有证券投资基金.*/,
//                "经证监会认可的比照公募基金管理的资管产品": /.*（不勾选）.*/,
                "债券正回购"          : /.*债券正回购.*/,
                "债券逆回购"          : /.*债券逆回购.*/,
                "债券正回购、债券逆回购"    : /.*债券回购.*|.*回购.*/,
                "融资融券"           : /.*融资融券.*|.*两融.*/,
                "存托凭证"           : /.*存托凭证.*/,
                "其他"      : /.*利率互换.*/,
                "股票一级、股票二级"           : /.*沪深交易所A股（含一级市场）.*/
        ]

        List<TaskElementVO.DrawResultVO> trueValueSet = new ArrayList<>();
        resultVOs.forEach({ drawResultOv ->
            String str = drawResultOv.text.replaceAll("\n", "").replaceAll("\r", "")
            Map matchMap = [:]
            for (item in REGEX_MAP) {
                if (str =~ "${item.value}") {
                    matchMap.put(str.indexOf("${item.value}"), "${item.key}")
                }
            }
            matchMap.forEach({ index, businessValue ->
                String[] arr = businessValue.split("、")
                if (arr.length == 2) {
                    drawResultOv.businessValue = arr[0]
                    drawResultOv.type = 1
                    trueValueSet.add(drawResultOv)
                    TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultOv)
                    objectNew.businessValue = arr[1]
                    objectNew.type = 1
                    trueValueSet.add(objectNew)
                } else {
                    drawResultOv.businessValue = businessValue
                    drawResultOv.type = 1
                    trueValueSet.add(drawResultOv)
                }
            })
        })
        Map<String, List<TaskElementVO.DrawResultVO>> group = trueValueSet.stream().collect(Collectors.groupingBy({ drawResultOv -> drawResultOv.businessValue }))
        List<TaskElementVO.DrawResultVO> voList = new ArrayList<>();
        group.forEach({ k, vl ->
            if (vl.size() > 1) {
                def sim=0.0
                int index=0
                for (int i = 0; i < vl.size(); i++) {
                    TaskElementVO.DrawResultVO draw= vl.get(i)
                    def  tmpSim=simi(draw.text, draw.businessValue)
                    if (tmpSim==1.0){
                        index=i
                        break
                    }
                    if (tmpSim>sim){
                        sim=tmpSim
                        index=i
                    }
                }
                voList.add(vl.get(index))
            }else {

                voList.add(vl.get(0))
            }
        })
        return voList
    }

    private static int min(int one, int two, int three) {
        int min = one;
        if (two < min) {
            min = two;
        }
        if (three < min) {
            min = three;
        }
        return min;
    }

    private static int ld(String str1, String str2) {

        int n = str1.length();
        int m = str2.length();
        int i; // 遍历str1的
        int j; // 遍历str2的
        char ch1; // str1的
        char ch2; // str2的
        int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
        if (n == 0) {
            return m;
        }
        if (m == 0) {
            return n;
        }
        def d = new int[n + 1][m + 1];
        for (i = 0; i <= n; i++) { // 初始化第一列
            d[i][0] = i;
        }
        for (j = 0; j <= m; j++) { // 初始化第一行
            d[0][j] = j;
        }
        for (i = 1; i <= n; i++) { // 遍历str1
            ch1 = str1.charAt(i - 1);
            // 去匹配str2
            for (j = 1; j <= m; j++) {
                ch2 = str2.charAt(j - 1);
                if (ch1 == ch2) {
                    temp = 0;
                } else {
                    temp = 1;
                }
                // 左边+1,上边+1, 左上角+temp取最小
                d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp);
            }
        }
        return d[n][m];
    }

    static double simi(String str1, String str2) {
        try {
            double ld = (double) ld(str1, str2);
            return (1 - ld / (double) Math.max(str1.length(), str2.length()));
        } catch (Exception e) {
            return 0.1;
        }
    }

//    public static void main(String[] args) {
//        String str1 = "测试12";
//        String str2 = "测试12";
//        System.out.println("ld=" + ld(str1, str2));
//        System.out.println("sim=" + sim(str1, str2));
//
//    }
}
